package Web.controller;


import Web.domain.ResponseResult;
import Web.domain.entity.Praise;
import Web.service.ArticleService;
import Web.service.PraiseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private PraiseService praiseService;

    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList() {

        ResponseResult result = articleService.hotArticleList();
        return result;
    }
    @ApiOperation("获取文章详情")
    @GetMapping("/{id}")
    public ResponseResult getArticleDetail(@PathVariable("id") Long id){
        return articleService.getArticleDetail(id);
    }
    @GetMapping("/newArticleList")
    public ResponseResult newArticleList(){
        ResponseResult result=articleService.newArticleList();
        return result;
    }
    @ApiOperation("更新文章浏览量")
    @PutMapping("/updateViewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable("id") Long id){
        return articleService.updateViewCount(id);
    }
    @ApiOperation("更新文章点赞量")
    @PutMapping("/updatePraiseCount")
    public ResponseResult setPraise(@RequestBody Praise pri){
        Integer result= praiseService.setPraise(pri);
        if(result==1){
            long change=1;
            return articleService.updatePraiseCount(pri.getArticle_id(),change);
        }
        else{
            long change=-1;
            return articleService.updatePraiseCount(pri.getArticle_id(),change);
        }
    }
    @ApiOperation("id用户的所有文章")
    @GetMapping("/user/{id}")
    public ResponseResult getUserArticle(@PathVariable("id")Long id){
        return articleService.getUserArticle(id);
    }
}